package cn.xyy.system.api;

import cn.xyy.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.xyy.common.domain.system.auth.RoleVO;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.xyy.common.annotation.log.Log;
import cn.xyy.common.domain.mvc.R;

/**
 * 角色接口
 * 
 * @author xyy
 *
 */
@RestController
@RequestMapping(value = "/rest/role")
public class RoleRest {

	@Autowired
	private RoleService roleService;

	@SaCheckPermission("role:list")
	@RequestMapping(value = "getListPage")
	public R getRolePage(RoleVO vo) {
		return R.ok(roleService.getRolePage(vo));
	}

	@SaCheckPermission("role:list")
	@RequestMapping(value = "getRoleList")
	public R getRoleList() {
		return R.ok(roleService.getEnableRoleList());
	}

	/**
	 * 添加角色
	 *
	 * @param role
	 * @return
	 */
	@SaCheckPermission("role:add")
	@Log("新增角色")
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public R add(@RequestBody RoleVO vo) {
		roleService.add(vo);
		return R.ok();
	}

	/**
	 * 修改角色
	 *
	 * @param role
	 * @return
	 */
	@SaCheckPermission("role:edit")
	@Log("修改角色")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public R update(@RequestBody RoleVO role) {
		roleService.update(role);
		return R.ok();
	}

	/**
	 * 移除角色
	 *
	 * @param id
	 * @return
	 */
	@SaCheckPermission("role:del")
	@Log("删除角色")
	@RequestMapping(value = "delete")
	public R delete(@RequestParam("id") String id) {
		roleService.delete(id);
		return R.ok();
	}

	/**
	 * 分配角色对应的数据权限
	 *
	 * @param roleRelationRightVO
	 * @return
	 */
	@SaCheckPermission("role:add")
	@Log("角色分配数据权限")
	@RequestMapping(value = "addDataScope", method = RequestMethod.POST)
	public R addDataScope(@RequestBody RoleVO role) {
		roleService.saveRelationDataScope(role);
		return R.ok();
	}

	@RequestMapping(value = "getDataScopeListByRole")
	public R getDataScopeListByRole(@RequestParam("roleId") String roleId) {
		return R.ok(roleService.getDataScopeListByRole(roleId));
	}

	@Log("更新角色状态")
	@SaCheckPermission("role:edit")
	@RequestMapping(value = "updateState", method = RequestMethod.POST)
	public R updateState(@RequestBody RoleVO role) {
		roleService.updateState(role);
		return R.ok();
	}

}
